package com.ctshk.rpc.order.custom.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.NumberConstant;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.common.utils.SnowflakeIdWorker;
import com.ctshk.rpc.order.custom.dto.tourist.CustomOrderTouristCertificateDTO;
import com.ctshk.rpc.order.custom.entity.CustomizedOrderTouristCertificate;
import com.ctshk.rpc.order.custom.mapper.CustomizedOrderTouristCertificateMapper;
import com.ctshk.rpc.order.custom.req.tourist.CustomOrderTouristCertificateAddReq;
import com.ctshk.rpc.order.custom.req.tourist.CustomOrderTouristCertificateDeleteReq;
import com.ctshk.rpc.order.custom.req.tourist.CustomOrderTouristCertificateListReq;
import com.ctshk.rpc.order.custom.service.ICustomizedOrderTouristCertificateService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 证件文件 服务实现类
 * </p>
 *
 * @author 叶冠峰
 * @since 2021-02-22
 */
@DubboService
public class CustomizedOrderTouristCertificateServiceImpl extends ServiceImpl<CustomizedOrderTouristCertificateMapper, CustomizedOrderTouristCertificate> implements ICustomizedOrderTouristCertificateService {

    @Autowired
    private CustomizedOrderTouristCertificateMapper orderTouristCertificateMapper;


    /**
     * 上載證件
     *
     * @param req
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result upload(List<CustomOrderTouristCertificateAddReq> req) {
        List<CustomizedOrderTouristCertificate> list = new ArrayList<>();
        req.forEach(addReq -> {
            CustomizedOrderTouristCertificate certificate = EntityUtil.copy(addReq, CustomizedOrderTouristCertificate.class);
            certificate.setId(SnowflakeIdWorker.nextId());
            certificate.setGmtCreate(LocalDateTime.now());
            certificate.setCreateId(addReq.getUserId());
            certificate.setIsDeleted(0);
            list.add(certificate);
        });
        boolean saveBatch = saveBatch(list);
        if (!saveBatch){
            throw new BusinessException(SystemError.SYS_437);
        }
        return Result.success();
    }

    /**
     * 證件列表
     *
     * @param req
     * @return
     */
    @Override
    public Result<List<CustomOrderTouristCertificateDTO>> list(CustomOrderTouristCertificateListReq req) {
        QueryWrapper<CustomizedOrderTouristCertificate> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CustomizedOrderTouristCertificate::getTouristId, req.getTouristId());
    //    queryWrapper.lambda().eq(CustomizedOrderTouristCertificate::getCertificateType,req.getCertificateType());
        queryWrapper.lambda().eq(CustomizedOrderTouristCertificate::getIsDeleted,0);
        List<CustomizedOrderTouristCertificate> certificateList = orderTouristCertificateMapper.selectList(queryWrapper);
        List<CustomOrderTouristCertificateDTO> certificateDTOList = EntityUtil.copyList(certificateList, CustomOrderTouristCertificateDTO.class);
        return Result.success(certificateDTOList);
    }

    /**
     * 删除
     *
     * @param req
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result delete(CustomOrderTouristCertificateDeleteReq req) {
        CustomizedOrderTouristCertificate orderTouristCertificate = orderTouristCertificateMapper.selectById(req.getId());
        if (orderTouristCertificate == null || Objects.equals(orderTouristCertificate.getIsDeleted(), NumberConstant.ONE.getValue())){
            throw new BusinessException(SystemError.SYS_411);
        }
        orderTouristCertificate.setIsDeleted(1);
        orderTouristCertificate.setModifiedId(req.getUserId());
        orderTouristCertificate.setGmtModified(LocalDateTime.now());
        int update = orderTouristCertificateMapper.updateById(orderTouristCertificate);
        if (update <= 0){
            throw new BusinessException(SystemError.SYS_437);
        }
        return Result.success(req.getId());
    }
}
